package peace.graphic.tween {
import flash.events.Event;

public class TweenQueue {
    protected var queue:Array = new Array();

    protected var playingTween:Tween;

    public function get playing():Boolean {
        return playingTween != null;
    }

    public function TweenQueue() {
    }

    public function addTween(tween:Tween, loop:Boolean = false, reverse:Boolean = false):void {
        tween.loop = loop;
        tween.reverse = reverse;
        queue.push(tween)
    }

    public function play():void {
        if (!playingTween) {
            execute();
        }
    }

    public function clear():void {
        stop();
        queue = [];
    }

    protected function execute(event:Event = null):void {
        if (playingTween) {
            playingTween.removeEventListener(TweenEvent.END, execute);
        }

        if (queue.length > 0) {
            playingTween = queue.shift();
        } else {
            playingTween = null;
        }

        if (playingTween) {
            playingTween.play(playingTween.loop, playingTween.reverse);
            playingTween.addEventListener(TweenEvent.END, execute, false, 0, true);
        }
    }


    public function stop():void {
        if (playingTween) {
            playingTween.stop();
            playingTween = null;
        }
    }

}
}